//
//  CircleModelData.m
//
//  Created by lianchuang  on 14-11-25
//  Copyright (c) 2014 __MyCompanyName__. All rights reserved.
//

#import "CircleModelData.h"


NSString *const kCircleModelDataDisplayorder = @"displayorder";
NSString *const kCircleModelDataLastposttitle = @"lastposttitle";
NSString *const kCircleModelDataModerators = @"moderators";
NSString *const kCircleModelDataIscollect = @"iscollect";
NSString *const kCircleModelDataMembers = @"members";
NSString *const kCircleModelDataDescription = @"description";
NSString *const kCircleModelDataLastpost = @"lastpost";
NSString *const kCircleModelDataPosts = @"posts";
NSString *const kCircleModelDataTitle = @"title";
NSString *const kCircleModelDataGid = @"gid";
NSString *const kCircleModelDataThreads = @"threads";
NSString *const kCircleModelDataIcon = @"icon";
NSString *const kCircleModelDataDateline = @"dateline";
NSString *const kCircleModelDataColor = @"color";
NSString *const kCircleModelDataModerator = @"moderator";
NSString *const kCircleModelDataPid = @"pid";
NSString *const kCircleModelDataTodayposts = @"todayposts";


@interface CircleModelData ()

- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;

@end

@implementation CircleModelData

@synthesize displayorder = _displayorder;
@synthesize lastposttitle = _lastposttitle;
@synthesize moderators = _moderators;
@synthesize iscollect = _iscollect;
@synthesize members = _members;
@synthesize dataDescription = _dataDescription;
@synthesize lastpost = _lastpost;
@synthesize posts = _posts;
@synthesize title = _title;
@synthesize gid = _gid;
@synthesize threads = _threads;
@synthesize icon = _icon;
@synthesize dateline = _dateline;
@synthesize color = _color;
@synthesize moderator = _moderator;
@synthesize pid = _pid;
@synthesize todayposts = _todayposts;


+ (instancetype)modelObjectWithDictionary:(NSDictionary *)dict
{
    return [[self alloc] initWithDictionary:dict];
}

- (instancetype)initWithDictionary:(NSDictionary *)dict
{
    self = [super init];
    
    // This check serves to make sure that a non-NSDictionary object
    // passed into the model class doesn't break the parsing.
    if(self && [dict isKindOfClass:[NSDictionary class]]) {
            self.displayorder = [self objectOrNilForKey:kCircleModelDataDisplayorder fromDictionary:dict];
            self.lastposttitle = [self objectOrNilForKey:kCircleModelDataLastposttitle fromDictionary:dict];
            self.moderators = [self objectOrNilForKey:kCircleModelDataModerators fromDictionary:dict];
            self.iscollect = [[self objectOrNilForKey:kCircleModelDataIscollect fromDictionary:dict] doubleValue];
            self.members = [self objectOrNilForKey:kCircleModelDataMembers fromDictionary:dict];
            self.dataDescription = [self objectOrNilForKey:kCircleModelDataDescription fromDictionary:dict];
            self.lastpost = [self objectOrNilForKey:kCircleModelDataLastpost fromDictionary:dict];
            self.posts = [self objectOrNilForKey:kCircleModelDataPosts fromDictionary:dict];
            self.title = [self objectOrNilForKey:kCircleModelDataTitle fromDictionary:dict];
            self.gid = [self objectOrNilForKey:kCircleModelDataGid fromDictionary:dict];
            self.threads = [self objectOrNilForKey:kCircleModelDataThreads fromDictionary:dict];
            self.icon = [self objectOrNilForKey:kCircleModelDataIcon fromDictionary:dict];
            self.dateline = [self objectOrNilForKey:kCircleModelDataDateline fromDictionary:dict];
            self.color = [self objectOrNilForKey:kCircleModelDataColor fromDictionary:dict];
            self.moderator = [self objectOrNilForKey:kCircleModelDataModerator fromDictionary:dict];
            self.pid = [self objectOrNilForKey:kCircleModelDataPid fromDictionary:dict];
            self.todayposts = [self objectOrNilForKey:kCircleModelDataTodayposts fromDictionary:dict];

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:self.displayorder forKey:kCircleModelDataDisplayorder];
    [mutableDict setValue:self.lastposttitle forKey:kCircleModelDataLastposttitle];
    NSMutableArray *tempArrayForModerators = [NSMutableArray array];
    for (NSObject *subArrayObject in self.moderators) {
        if([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
            // This class is a model object
            [tempArrayForModerators addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
        } else {
            // Generic object
            [tempArrayForModerators addObject:subArrayObject];
        }
    }
    [mutableDict setValue:[NSArray arrayWithArray:tempArrayForModerators] forKey:kCircleModelDataModerators];
    [mutableDict setValue:[NSNumber numberWithDouble:self.iscollect] forKey:kCircleModelDataIscollect];
    [mutableDict setValue:self.members forKey:kCircleModelDataMembers];
    [mutableDict setValue:self.dataDescription forKey:kCircleModelDataDescription];
    [mutableDict setValue:self.lastpost forKey:kCircleModelDataLastpost];
    [mutableDict setValue:self.posts forKey:kCircleModelDataPosts];
    [mutableDict setValue:self.title forKey:kCircleModelDataTitle];
    [mutableDict setValue:self.gid forKey:kCircleModelDataGid];
    [mutableDict setValue:self.threads forKey:kCircleModelDataThreads];
    [mutableDict setValue:self.icon forKey:kCircleModelDataIcon];
    [mutableDict setValue:self.dateline forKey:kCircleModelDataDateline];
    [mutableDict setValue:self.color forKey:kCircleModelDataColor];
    [mutableDict setValue:self.moderator forKey:kCircleModelDataModerator];
    [mutableDict setValue:self.pid forKey:kCircleModelDataPid];
    [mutableDict setValue:self.todayposts forKey:kCircleModelDataTodayposts];

    return [NSDictionary dictionaryWithDictionary:mutableDict];
}

- (NSString *)description 
{
    return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
}

#pragma mark - Helper Method
- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict
{
    id object = [dict objectForKey:aKey];
    return [object isEqual:[NSNull null]] ? nil : object;
}


#pragma mark - NSCoding Methods

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];

    self.displayorder = [aDecoder decodeObjectForKey:kCircleModelDataDisplayorder];
    self.lastposttitle = [aDecoder decodeObjectForKey:kCircleModelDataLastposttitle];
    self.moderators = [aDecoder decodeObjectForKey:kCircleModelDataModerators];
    self.iscollect = [aDecoder decodeDoubleForKey:kCircleModelDataIscollect];
    self.members = [aDecoder decodeObjectForKey:kCircleModelDataMembers];
    self.dataDescription = [aDecoder decodeObjectForKey:kCircleModelDataDescription];
    self.lastpost = [aDecoder decodeObjectForKey:kCircleModelDataLastpost];
    self.posts = [aDecoder decodeObjectForKey:kCircleModelDataPosts];
    self.title = [aDecoder decodeObjectForKey:kCircleModelDataTitle];
    self.gid = [aDecoder decodeObjectForKey:kCircleModelDataGid];
    self.threads = [aDecoder decodeObjectForKey:kCircleModelDataThreads];
    self.icon = [aDecoder decodeObjectForKey:kCircleModelDataIcon];
    self.dateline = [aDecoder decodeObjectForKey:kCircleModelDataDateline];
    self.color = [aDecoder decodeObjectForKey:kCircleModelDataColor];
    self.moderator = [aDecoder decodeObjectForKey:kCircleModelDataModerator];
    self.pid = [aDecoder decodeObjectForKey:kCircleModelDataPid];
    self.todayposts = [aDecoder decodeObjectForKey:kCircleModelDataTodayposts];
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{

    [aCoder encodeObject:_displayorder forKey:kCircleModelDataDisplayorder];
    [aCoder encodeObject:_lastposttitle forKey:kCircleModelDataLastposttitle];
    [aCoder encodeObject:_moderators forKey:kCircleModelDataModerators];
    [aCoder encodeDouble:_iscollect forKey:kCircleModelDataIscollect];
    [aCoder encodeObject:_members forKey:kCircleModelDataMembers];
    [aCoder encodeObject:_dataDescription forKey:kCircleModelDataDescription];
    [aCoder encodeObject:_lastpost forKey:kCircleModelDataLastpost];
    [aCoder encodeObject:_posts forKey:kCircleModelDataPosts];
    [aCoder encodeObject:_title forKey:kCircleModelDataTitle];
    [aCoder encodeObject:_gid forKey:kCircleModelDataGid];
    [aCoder encodeObject:_threads forKey:kCircleModelDataThreads];
    [aCoder encodeObject:_icon forKey:kCircleModelDataIcon];
    [aCoder encodeObject:_dateline forKey:kCircleModelDataDateline];
    [aCoder encodeObject:_color forKey:kCircleModelDataColor];
    [aCoder encodeObject:_moderator forKey:kCircleModelDataModerator];
    [aCoder encodeObject:_pid forKey:kCircleModelDataPid];
    [aCoder encodeObject:_todayposts forKey:kCircleModelDataTodayposts];
}

- (id)copyWithZone:(NSZone *)zone
{
    CircleModelData *copy = [[CircleModelData alloc] init];
    
    if (copy) {

        copy.displayorder = [self.displayorder copyWithZone:zone];
        copy.lastposttitle = [self.lastposttitle copyWithZone:zone];
        copy.moderators = [self.moderators copyWithZone:zone];
        copy.iscollect = self.iscollect;
        copy.members = [self.members copyWithZone:zone];
        copy.dataDescription = [self.dataDescription copyWithZone:zone];
        copy.lastpost = [self.lastpost copyWithZone:zone];
        copy.posts = [self.posts copyWithZone:zone];
        copy.title = [self.title copyWithZone:zone];
        copy.gid = [self.gid copyWithZone:zone];
        copy.threads = [self.threads copyWithZone:zone];
        copy.icon = [self.icon copyWithZone:zone];
        copy.dateline = [self.dateline copyWithZone:zone];
        copy.color = [self.color copyWithZone:zone];
        copy.moderator = [self.moderator copyWithZone:zone];
        copy.pid = [self.pid copyWithZone:zone];
        copy.todayposts = [self.todayposts copyWithZone:zone];
    }
    
    return copy;
}


@end
